package spectra.ee.web.km.service;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.map.CaseInsensitiveMap;

import com.ibatis.sqlmap.client.SqlMapClient;
import spectra.base.log.Logger;
import spectra.base.sql.ibatis.SqlMapperWeb;
import spectra.base.util.DateUtil;
import spectra.base.util.StringUtil;
import spectra.base.util.Util;
import spectra.ee.web.WebPublic;
import spectra.ee.web.common.service.AttachManager;
import spectra.ee.web.kb.model.TopKeyword;
import spectra.ee.web.kbhelper.model.KbHelper;
import spectra.ee.web.km.model.ChangeHistory;
import spectra.ee.web.km.model.ChangeHistoryAttach;
import spectra.ee.web.km.model.ChangeHistoryContents;
import spectra.ee.web.km.model.ChangeHistoryItem;

/**
 * 변경관리 관련 클래스.
 *
 * @author kmhan
 */
public class ChangeHistoryServiceBase
{

    /** Logger객체. */
    private static Logger logger = Logger.getLoggerInstance(ChangeHistoryServiceBase.class.getName());

    /** SqlMapClient 객체. */
    protected static SqlMapClient sqlMap = SqlMapperWeb.getSqlMapWebInstance();
        

    public List selectChangeHistoryListByKbId(String kbId)
    {
    	return selectChangeHistoryListByKbId(kbId, null);
    }
    /**
     * 지식아이디로 변경이력에 대해서 가져오는 메소드이다.
     * @param kbId
     * @return
     */
    public List selectChangeHistoryListByKbId(String kbId, String orderBy)
    {
        List list = null;
        try
        {
            ChangeHistory obj = new ChangeHistory();
            obj.setKbId(kbId);
            obj.setProcessSeq(0);
            obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
            if (orderBy != null)
            	obj.setOrderBy(orderBy);
            list = (List) sqlMap.queryForList("ChangeHistory.selectChangeHistoryListByKbId", obj);
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
        }
        return list;
    }
    
    /**
     * 지식아이디, 프로세스 순번, 히스토리 순번으로 변경이력 가져오는 메소드이다.
     * @param kbId
     * @param processSeq
     * @param historySeq
     * @return
     */
    public ChangeHistory selectChangeHistoryListByVersionInfo(String kbId, int processSeq, String versionInfo)
    {
    	ChangeHistory changeHitoryObj = null;
        try
        {
            ChangeHistory obj = new ChangeHistory();
            obj.setKbId(kbId);
            obj.setProcessSeq(processSeq);
            obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
            obj.setVersionInfo(versionInfo);
            changeHitoryObj = (ChangeHistory) sqlMap.queryForObject("ChangeHistory.selectChangeHistoryListByVersionInfo", obj);
            if (changeHitoryObj != null)
            {
            	String contents = selectChangeHistoryContents(kbId, processSeq, changeHitoryObj.getHistorySeq());
            	changeHitoryObj.setContents(contents);
  	   			 
  	   			List attachList = selectChangeHistoryAttach(kbId, processSeq, changeHitoryObj.getHistorySeq());
  	   			changeHitoryObj.setAttachList(attachList);
  	   			
  	   			List itemList = selectChangeHistoryItem(kbId, processSeq, changeHitoryObj.getHistorySeq());
  	   			if (itemList != null && itemList.size() > 0)
  	   			{
  	   				for (int i=0; i<itemList.size(); i++)
  	   				{
  	   					CaseInsensitiveMap map = (CaseInsensitiveMap) itemList.get(i);
  	   					if ("node_id".equals(map.get("item_type")+"")) // 노드아이디
  	   						changeHitoryObj.setNodeId(map.get("item_value")+""); 
  	   					else if ("node_path".equals(map.get("item_type")+"")) // 노드 경로
  	   						changeHitoryObj.setNodePath(map.get("item_value")+"");  	   					
  	   				}
  	   			}
            }
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
        }
        return changeHitoryObj;
    }
    
    /**
     * 변경이력의 내용을 가져오는 메소드이다.
     * @param kbId
     * @param processSeq
     * @param historySeq
     * @return
     */
    public String selectChangeHistoryContents(String kbId, int processSeq, int historySeq)
    {
        String strContents = "";
        List list = null;
        try
        {
        	ChangeHistoryContents obj = new ChangeHistoryContents();
        	obj.setKbId(kbId);
        	obj.setProcessSeq(processSeq);
        	obj.setHistorySeq(historySeq);
        	obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
            list = (List)sqlMap.queryForList("ChangeHistory.selectChangeHistoryContents", obj);
            if (list != null && list.size() > 0)
            {
                CaseInsensitiveMap map = null;

                for (int i = 0; i < list.size(); i++)
                {
                    map = (CaseInsensitiveMap)list.get(i);
                    strContents += map.get("CONTENTS");
                }
            }
        }
        catch (SQLException e)
        {
            logger.error(e.getMessage());
        }
        return strContents;
    }
    
    /**
     * 변경이력의 첨부파일을 가져오는 메소드이다.
     * @param kbId
     * @param processSeq
     * @param historySeq
     * @return
     */
    public List selectChangeHistoryAttach(String kbId, int processSeq, int historySeq)
    {
    	List attachList = null;
        try
        {
        	ChangeHistoryAttach obj = new ChangeHistoryAttach();
        	obj.setKbId(kbId);
        	obj.setProcessSeq(processSeq);
        	obj.setHistorySeq(historySeq);
        	obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
        	attachList = (List)sqlMap.queryForList("ChangeHistory.selectChangeHistoryAttachList", obj);            
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
        }
        return attachList;
    }
    
    /**
     * 변경이력의 항목을 가져오는 메소드이다. (노드아이디, 노드경로)
     * @param kbId
     * @param processSeq
     * @param historySeq
     * @return
     */
    public List selectChangeHistoryItem(String kbId, int processSeq, int historySeq)
    {
    	List itemList = null;
        try
        {
        	ChangeHistoryItem obj = new ChangeHistoryItem();
        	obj.setKbId(kbId);
        	obj.setProcessSeq(processSeq);
        	obj.setHistorySeq(historySeq);
        	obj.setServiceType(WebPublic.SERVICE_TYPE_KB);
        	itemList = (List)sqlMap.queryForList("ChangeHistory.selectChangeHistoryItemList", obj);            
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
        }
        return itemList;
    }
   
}
